<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Member_center extends CI_Controller
{
	public $userInfo;
	public function _load()
	{
		$this->load->model('User_model');
		$this->load->model('My_address_model');
		$this->load->model('Record_model');
		$this->load->library('user_verification');
		$this->load->library('finance_func');
	}
	function __construct()
	{
		parent::__construct();
		$this->_load();
		$is_login = $this->user_verification->is_login();
		if($is_login == 0)
		{
			redirect('sign/login', 'location', 303);
		}
		$userInfo = $this->User_model->getForId($this->input->cookie('user_id', TRUE));
		$this->userInfo = $userInfo[0];
	}
	public function index()
	{
		$this->profile();
	}
	/**
	 * 个人信息*/
	public function profile()
	{
		$data['userInfo'] = $this->userInfo;
		if(isset($_GET['parm']) || !empty($_GET['parm']))
		{
			if($_GET['parm'] != '')
			{
				$data['tips'] = $this->common_func->returnHtml($_GET['parm']);
			}
		}
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/member_center/profile');
		$this->load->view('member/public/foot');
	}
	public function editProfile()
	{
		$this->db->trans_begin();
		$data['user_id'] = $_REQUEST['user_id'];
		$data['name'] = $_REQUEST['name'];
		$data['email'] = $_REQUEST['email'];
		$data['website'] = $_REQUEST['website'];
		$data['about'] = $_REQUEST['about'];
		if($_FILES['userfile']['name'] != '' || !empty($_FILES['userfile']['name']))
		{
			$config['upload_path'] = './upload/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['max_size'] = '2000';
			$config['encrypt_name'] = true;
			$this->load->library('upload', $config);
			$this->upload->do_upload();
			$fileInfo = $this->upload->data();
			print_r($fileInfo);
			$data['avatar'] = $config['upload_path'].$fileInfo['file_name'];
		}
		$this->User_model->updateUser($data);
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
			$data['pay_result'] = array(
									'tips_img'=>'img/alert.gif',
									'tips_text'=>'<span style="color: rgb(241, 132, 31);">错误提示：系统故障！！！.</span><span>很抱歉的通知您，系统当前故障，该操作被意外终止，请稍后再进行该操作...</span>',
					);
			$this->common_func->publicView($data);
		}
		else
		{
			$this->db->trans_commit();
			redirect('member/member_center/profile?parm=0.0.2.0.0.0', 'location', 303);
		}
	}
	public function editPassWord()
	{
		$this->userInfo['pwd'] = $_REQUEST['pwd'];
		if($this->user_verification->is_verifica($this->userInfo) == 1)
		{
			$this->db->trans_begin();
			$data['pwd'] = $_REQUEST['new_pwd'];
			$this->User_model->updateUser($data);
			if ($this->db->trans_status() === FALSE)
			{
			    $this->db->trans_rollback();
				$data['pay_result'] = array(
										'tips_img'=>'img/alert.gif',
										'tips_text'=>'<span style="color: rgb(241, 132, 31);">错误提示：系统故障！！！.</span><span>很抱歉的通知您，系统当前故障，该操作被意外终止，请稍后再进行该操作...</span>',
						);
				$this->common_func->publicView($data);
			}
			else
			{
			    $this->db->trans_commit();
			    redirect('member/member_center/profile?parm=0.0.2.0.1.0', 'location', 303);
			}
		}
		else
		{
			redirect('member/member_center/profile?parm=0.0.2.0.1.1', 'location', 303);
		}
	}
	public function editTradecode()
	{
		if($_REQUEST['sys_type'] == 'new')
		{
			$this->db->trans_begin();
			$data['user_id'] = $_REQUEST['user_id'];
			$data['tradecode'] = $this->common_func->createTradecode($_REQUEST['new_tradecode']);
			$this->User_model->updateUser($data);
			if ($this->db->trans_status() === FALSE)
			{
			    $this->db->trans_rollback();
				$data['pay_result'] = array(
										'tips_img'=>'img/alert.gif',
										'tips_text'=>'<span style="color: rgb(241, 132, 31);">错误提示：系统故障！！！.</span><span>很抱歉的通知您，系统当前故障，该操作被意外终止，请稍后再进行该操作...</span>',
						);
				$this->common_func->publicView($data);
			}
			else
			{
			    $this->db->trans_commit();
			    redirect('member/member_center/profile?parm=0.0.2.0.2.0', 'location', 303);
			}
		}
		else  if($_REQUEST['sys_type'] == 'edit')
		{
			$this->userInfo['tradecode'] = $_REQUEST['new_tradecode'];
			if($this->user_verification->is_tradecode($this->userInfo) == 1)
			{
				$this->db->trans_begin();
				$data['user_id'] = $_REQUEST['user_id'];
				$data['tradecode'] = $this->common_func->createTradecode($_REQUEST['new_tradecode']);
				$this->User_model->updateUser($data);
				if ($this->db->trans_status() === FALSE)
				{
				    $this->db->trans_rollback();
					$data['pay_result'] = array(
											'tips_img'=>'img/alert.gif',
											'tips_text'=>'<span style="color: rgb(241, 132, 31);">错误提示：系统故障！！！.</span><span>很抱歉的通知您，系统当前故障，该操作被意外终止，请稍后再进行该操作...</span>',
							);
					$this->common_func->publicView($data);
				}
				else
				{
				    $this->db->trans_commit();
				    redirect('member/member_center/profile?parm=0.0.2.0.2.1', 'location', 303);
				}
			}
			else
			{
				redirect('member/member_center/profile?parm=0.0.2.0.2.2', 'location', 303);
			}
		}
	}
	/**
	 * 账户余额*/
	public function finance()
	{
		$data['userInfo'] = $this->userInfo;
		$record_type = (isset($_GET['list']))?array('record_type'=>0,'record_time'=>0):array('record_type'=>0,'record_time'=>1);
		$data['table_hover'] = (isset($_GET['list']))?0:1;
		$data['recordData'] = $this->Record_model->getRecordData($record_type);
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/member_center/finance');
		$this->load->view('member/public/foot');
	}
	public function addFinance()
	{
		$data['userInfo'] = $this->userInfo;
		if(isset($_GET['parm']) || !empty($_GET['parm']))
		{
			if($_GET['parm'] != '')
			{
				$data['tips'] = $this->common_func->returnHtml($_GET['parm']);
			}
		}
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/member_center/add_finance');
		$this->load->view('member/public/foot');
	}
	public function getAddFinanceInfo()
	{
		$data = $this->Record_model->getAddFinanceInfo();
		echo json_encode($data);
	}
	public function doAddFinance()
	{
		$data['userInfo'] = $this->userInfo;
		if($_POST['tb_order_no'] == '')
		{
			redirect('member/member_center/addFinance?parm=0.4.2.1.0.0', 'location', 303);
		}
		if($_POST['tb_order_name'] == '')
		{
			redirect('member/member_center/addFinance?parm=0.4.2.1.0.1', 'location', 303);
		}
		$orderData = $this->common_func->isTbOrder($_POST);
		switch ($orderData['code'])
		{
			case -1:
				redirect('member/member_center/addFinance?parm=0.4.2.1.0.2', 'location', 303);
				break;
			case 1:
				$this->db->trans_begin();
				/*执行充值操作*/
				$this->finance_func->addFinance($orderData['data'],$this->userInfo);
				if ($this->db->trans_status() === FALSE)
				{
					$this->db->trans_rollback();
					$data['pay_result'] = array(
											'tips_img'=>'img/alert.gif',
											'tips_text'=>'<span style="color: rgb(241, 132, 31);">错误提示：系统故障！！！.</span><span>很抱歉的通知您，系统当前故障，该操作被意外终止，请稍后再进行该操作...</span>',
							);
					$this->common_func->publicView($data);
				}
				else
				{
					$this->db->trans_commit();
					redirect('/member/member_center/finance', 'location', 303);
				}
				break;
			case 0:
				redirect('member/member_center/addFinance?parm=0.4.2.1.0.3', 'location', 303);
				break;
			case -2:
				$data['pay_result'] = $orderData['info'];
				$this->common_func->publicView($data);
				break;
		}
	}
	public function pass_name()
	{
		echo $this->user_verification->is_user_name($_POST['name']);
	}
	public function consume()
	{
		$data['userInfo'] = $this->userInfo;
		$record_type = (isset($_GET['list']))?array('record_type'=>1,'record_time'=>0):array('record_type'=>1,'record_time'=>1);
		$data['table_hover'] = (isset($_GET['list']))?0:1;
		$data['recordData'] = $this->Record_model->getRecordData($record_type);
		$data['consumeNum'] = $this->Record_model->getConsumeNum($record_type);
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/member_center/consume');
		$this->load->view('member/public/foot');
	}
	public function score()
	{
		$data['userInfo'] = $this->userInfo;
		$record_type = (isset($_GET['list']))?array('record_type'=>2,'record_time'=>0):array('record_type'=>2,'record_time'=>1);
		$data['table_hover'] = (isset($_GET['list']))?0:1;
		$data['recordData'] = $this->Record_model->getRecordData($record_type);
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/member_center/score');
		$this->load->view('member/public/foot');
	}
	/**
	 * 发货地址管理*/
	public function myAddress()
	{
		$data['userInfo'] = $this->userInfo;
		$data['myAddressData'] = $this->My_address_model->getMyAddress($this->userInfo);
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/member_center/myAddress');
		$this->load->view('member/public/foot');
	}
	public function addMyAddress()
	{
		echo $this->My_address_model->addMyAddress($_POST);
	}
	public function editMyAddress()
	{
		$this->My_address_model->editMyAddress($_POST);
	}
	public function deleteMyAddress()
	{
		echo $this->My_address_model->deleteMyAddress($_POST);
	}
	public function doOauth()
	{
		$this->common_func->oauthTb();
	}
	public function getOrderInfo()
	{
		$orderInfo = $this->common_func->getOrderInfoForId('226717179835217');
		print_r($orderInfo);
	}
	public function refreshToken()
	{
		$this->common_func->refreshAccessToken();
	}
}